package lightswitch.server.code.sqlhelper.expression.operators.conditional;

import java.util.List;

import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class AndExpression extends BinaryExpression {
	
	public AndExpression(Expression left, Expression right, boolean isNot)
	{
					this.leftExpression = left;
			this.rightExpression=right;
			this.isNot=isNot;
			
	
	}
	
	public AndExpression(String left, String right, boolean isNot, From _from)
	{
				
		leftExpression = ExpressionHelper.parseExpression(left,_from);
		rightExpression = ExpressionHelper.parseExpression(right,_from);
			this.isNot=isNot;
			
	
	}
	public String toString()
	{
		return "("+leftExpression.toString() + " AND " + rightExpression.toString() +" )";
	}
	
	public Object GetValue(DataRow row, List<From> parentFromList)
	{
		
		dataType = TypeOfData.BOOLEAN;
		Object left = leftExpression.GetValue(row,parentFromList);
		Object right = rightExpression.GetValue(row,parentFromList);

		String type1 = leftExpression.dataType;
		String type2 = rightExpression.dataType;
		
		if(!type1.equals(TypeOfData.BOOLEAN) || !type2.equals(TypeOfData.BOOLEAN))
		{
			//throw exception
			return null;
		}
		else
		{
			boolean b1 = ((boolean)left)==true?true : false;
			boolean b2 = ((boolean)right)==true?true : false;
			return b1&&b2;
		}
	
	}
	
}
